package com.hfjyz.system.category.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.hfjyz.base.action.GridBaseAction;
import com.hfjyz.base.exception.BaseException;
import com.hfjyz.system.category.bean.CategoryBean;
import com.hfjyz.system.category.service.CategoryService;

public class CategoryAction extends GridBaseAction {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -6292139143485315414L;
    private CategoryBean data = null;
    private String selectIds = null;
    private Integer parentId = null;

    private CategoryService service = null;

    public CategoryService getService() {
        return service;
    }

    public void setService(CategoryService service) {
        this.service = service;
    }

    public CategoryBean getData() {
        return data;
    }

    public void setData(CategoryBean data) {
        this.data = data;
    }

    public String getSelectIds() {
        return selectIds;
    }

    public void setSelectIds(String selectId) {
        this.selectIds = selectId;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    @Override
    protected List<CategoryBean> getBeanList() throws BaseException {
        return service.findCategory(getSearchCondition(), parentId);
    }

    @Override
    protected long getTotalCount() throws BaseException {
        return service.getCategoryCount(getSearchCondition(), parentId);
    }

    public String save() {
        map = new HashMap<String, Object>();
        try {
            service.saveCategory(data);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String delete() {
        map = new HashMap<String, Object>();
        try {
            String[] idArray = selectIds.split(",");
            List<Integer> idList = new ArrayList<Integer>();
            for (String id : idArray) {
                idList.add(Integer.valueOf(id));
            }
            service.deleteCategory(idList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String updateInit() {
        map = new HashMap<String, Object>();
        try {
            Integer id = Integer.valueOf(selectIds);
            data = service.updateInitCategory(id);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }
    
    public String initLevel() {
        map = new HashMap<String, Object>();
        try {
            List<CategoryBean> categoryList = service.findCategory(parentId);
            map.put("categorys", categoryList);
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
        return SUCCESS;
    }

    public String execute() {
        return SUCCESS;
    }
}
